import * as XLSX from 'xlsx';
import fs from 'fs';

export const createExcel = (name: string, data: any) => {
    // 创建一个新的工作簿
    const wb = XLSX.utils.book_new();

    // 创建一个新的工作表
    // const ws_data = [['Hello', 'World']];
    const ws = XLSX.utils.aoa_to_sheet(data);

    // 将工作表添加到工作簿
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet 1');

    // 将工作簿写入文件
    XLSX.writeFile(wb, `${name}.xlsx`);
};

export const getExcelData = (filePath: string) => {
  const fileContent = fs.readFileSync(filePath);
  const workbook = XLSX.read(fileContent);
  const sheetName = workbook.SheetNames[0];
  const worksheet = workbook.Sheets[sheetName];
  const jsonData = XLSX.utils.sheet_to_json(worksheet);
  return jsonData;
}

export const delay = (ms: number) => {
    return new Promise(resolve => setTimeout(resolve, ms));
}